package com.momo.seckilling.EventModel;


import com.momo.seckilling.strategy.SeckillOperator;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by li on 2019/3/18.
 */
public class SeckillEventProcessor implements Handle{
    private CentralEventProcessor centralEventProcessor;
    private StateMachine stateMachine = new StateMachine();
    public SeckillEventProcessor(CentralEventProcessor centralEventProcessor){
        this.centralEventProcessor = centralEventProcessor;
    }

    @Override
    public void handle(Event event) {
        try {
            SeckillEvent seckillEvent = (SeckillEvent)event;
            StateProcess stateProcess = this.stateMachine.getProcessBySateType(seckillEvent);
            stateProcess.process(seckillEvent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class StateMachine {
        private Map<SeckillEventType,StateProcess> processColletion = new HashMap<SeckillEventType,StateProcess>();
        public StateMachine(){
            processColletion.put(SeckillEventType.NEW,new NewStateProcess());
            processColletion.put(SeckillEventType.COMPLETE,new CompleteStateProcess());
            processColletion.put(SeckillEventType.FAIL,new FailStateProcess());
        }
        public StateProcess getProcessBySateType(SeckillEvent seckillEvent){
            SeckillEventType seckillEventType = seckillEvent.getSeckillEventType();
            StateProcess stateProcess = processColletion.get(seckillEventType);
            return stateProcess;
        }
    }

    public class NewStateProcess implements StateProcess {
        public void process(Event event){
            System.out.println("开始秒杀处理");
            SeckillEvent seckillEvent = (SeckillEvent) event;
            SeckillOperator seckillOperator = seckillEvent.getSeckillOperator();
            int userid = seckillEvent.getUserid();
            int id = seckillEvent.getId();
            seckillOperator.seckillBy(userid,id);
            Event eventresult = new SeckillEvent("complete",SeckillEventType.COMPLETE,seckillOperator,userid,id);
            try {
                centralEventProcessor.centralQueue.put(eventresult);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public class CompleteStateProcess implements StateProcess {
        public void process(Event event){
            System.out.println("结束秒杀处理");
            Event eventresult = new OrderEvent("new",OrderEventType.NEW);
            try {
                centralEventProcessor.centralQueue.put(eventresult);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public class FailStateProcess implements StateProcess {
        public void process(Event event){
            System.out.println("秒杀异常处理");
        }
    }

}
